#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <uio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstdio>
#include "SysCall.hpp"

SysCall::SysCall(){
  start = 0;
  retVal = 0;
}

SysCall::SysCall(unsigned long s, int v){
  start = s;
  retVal = v;
}

int SysCall::runCall(){
  return 1;
}

unsigned long SysCall::getStart(){
  return start;
}

int SysCall::getRetVal(){
  return retVal;
}

SysRead::SysRead(unsigned long s, int v, int f, const int n): SysCall(s, v){
  fd = f;
  //char b[n];
  //buf = &b[0];
  num = n;
}

int SysRead::runCall(){
  //ssize_t read(int fd, void *buf, size_t count);
  //printf("READ CALL %d %s %d\n", fd, buf, num);
  int r = read(fd, buf, num);
  printf("Replay: DATA READ:\n\n%s\n", buf);
  return r;
}

SysPRead::SysPRead(unsigned long s, int v, int f, const int n, int o): SysCall(s, v){
  fd = f;
  //char b[n];
  //buf = &b[0];
  num = n;
  off = o; 
}

int SysPRead::runCall(){
  //ssize_t pread(int fd, void *buf, size_t count, off_t offset);
  int r = pread(fd, buf, num, off);     
  printf("Replay: DATA READ:\n\n%s\n", buf);
  return r;
}

SysWrite::SysWrite(unsigned long s, int v, int f, const int n): SysCall(s, v){
  fd = f;
  memset(buf, '\0', 1000);
  for(int i = 1; i < v; i++)
    strcat(buf, "a");
  strcat(buf, "\n");
  num = n;
}

int SysWrite::runCall(){
  //ssize_t write(int fd, const void *buf, size_t count);
  //printf("WRITE CALL %d %s %d\n", fd, buf, num);
  return write(fd, buf, num);
}

SysPWrite::SysPWrite(unsigned long s, int v, int f, const int n, int o): SysCall(s, v){
  fd = f;
  //char b[n];
  //buf = &b[0];
  memset(buf, '\0', 1000);
  for(int i = 1; i < v; i++)
    strcat(buf, "a");
  strcat(buf, "\n");
  num = n;
  off = o; 
}

int SysPWrite::runCall(){
  //ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
  return pwrite(fd, buf, num, off);
}

SysUnlink::SysUnlink(unsigned long s, int v, char * p): SysCall(s, v){
  strcpy(path, p);
}

int SysUnlink::runCall(){
  //int unlink(const char *pathname);
  return unlink(path);
}


SysOpen::SysOpen(unsigned long s, int v, char * p, int f, int m): SysCall(s, v){
  //printf("%s\n", p);
  strcpy(path, p);
  flags = f;
  mode = m;
}

int SysOpen::runCall(){
  //int open(const char *pathname,int flags, mode_t mode);
  //printf("OPEN CALL %s %d %d\n", path, flags, mode);
  return open(path, flags, mode);
}

SysClose::SysClose(unsigned long s, int v, int f): SysCall(s, v){
  fd = f;
}

int SysClose::runCall(){
  //int close(int fd);
  return close(fd);
}

SysCreat::SysCreat(unsigned long s, int v, char * p, int m): SysCall(s, v){
  strcpy(path, p);
  mode = m;
}

int SysCreat::runCall(){
  //int creat(const char *pathname, mode_t mode);
  return creat(path, mode);
}

SysWritev::SysWritev(unsigned long s, int v): SysCall(s, v){

}

int SysWritev::runCall(){
  //ssize_t writev(int fd, const struct iovec *vector, int count);
  return 0;
}

SysReadv::SysReadv(unsigned long s, int v): SysCall(s, v){

}

int SysReadv::runCall(){
  //ssize_t readv(int fd, const struct iovec *vector, int count);
  return 0;
}

SysLink::SysLink(unsigned long s, int v, char * o, char * n): SysCall(s, v){
  strcpy(oldp, o);
  strcpy(newp, n);
}

int SysLink::runCall(){
  //int link(const char *oldpath, const char *newpath);
  return link(oldp, newp);
}

SysSymlink::SysSymlink(unsigned long s, int v, char * o, char * n): SysCall(s, v){
  strcpy(oldp, o);
  strcpy(newp, n);
}

int SysSymlink::runCall(){
  //int symlink(const char *oldpath, const char *newpath);
  return symlink(oldp, newp);
}

SysMkdir::SysMkdir(unsigned long s, int v, char * p, int m): SysCall(s, v){
  strcpy(path, p);
  mode = m;
}

int SysMkdir::runCall(){
  //int mkdir(const char *pathname, mode_t mode);
  //printf("MKDIR CALL %s %d\n", path, mode);
  return mkdir(path, mode);
}

SysRmdir::SysRmdir(unsigned long s, int v, char * p): SysCall(s, v){
  strcpy(path, p);
}

int SysRmdir::runCall(){
  //int rmdir(const char *pathname);
  return rmdir(path);
}

SysMknod::SysMknod(unsigned long s, int v, char * p, int m, int d): SysCall(s, v){
  strcpy(path, p);
  mode = m;
  dev = d;
}

int SysMknod::runCall(){
  //int mknod(const char *pathname, mode_t mode, dev_t dev);
  return mknod(path, mode, dev);
}

SysRename::SysRename(unsigned long s, int v): SysCall(s, v){

}

int SysRename::runCall(){
  return 0;
}

SysChdir::SysChdir(unsigned long s, int v): SysCall(s, v){

}

int SysChdir::runCall(){
  return 0;
}
